# Copyright (c) 2018 Presto Labs Pte. Ltd.
# Author: jaewon, inkyu

from absl import app, flags
from tornado.ioloop import IOLoop

from coin.proto.coin_order_gateway_pb2 import OrderGatewayLog
from coin.util.log_watcher.proto_log_watcher import ProtoLogWatcher
from google.protobuf.json_format import MessageToDict


def on_log(path, record):
  pb = OrderGatewayLog()
  pb.ParseFromString(record.data)
  print(path, MessageToDict(pb))


def main(argv=None):
  argv = flags.FLAGS(argv)

  try:
    ioloop = IOLoop()
    watcher = ProtoLogWatcher(target_dir='/home/ec2-user/data/og_proto_log', on_log=on_log)
    watcher.start(ioloop)
    ioloop.start()
    watcher.stop()
    return 0
  except (KeyboardInterrupt, SystemExit):
    print
    return 1


if __name__ == "__main__":
  app.run(main)
